<?php

namespace app\api\controller;

use app\admin\model\litestore\Litestoreorder;
use app\admin\model\Orderday;
use app\admin\model\Userrelation;
use app\admin\model\Usersales;
use app\api\service\Orderservice;
use app\api\service\RankService;
use app\common\controller\Api;

/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     *
     */
    public function index()
    {

        $relArray = explode(",", 1);
        $this->success('请求成功', $ref_uid = $relArray[0]);
    }


    public function testVipProfit()
    {
        $literOrder = new \app\admin\model\litestore\Litestoreorder();
        $order = $literOrder->where('order_no', '2021012815560553101481281031')->find();
        $orderService = new Orderservice();
        $orderService->cacRefAward($order);
    }

    public function testOrderProfit()
    {
        $literOrder = new \app\admin\model\litestore\Litestoreorder();
        $order = $literOrder->where('order_no', '2021032321440351525497457871')->find();
        $orderService = new Orderservice();
        $orderService->cacOrderAward($order);
    }

    public function testUpdateUser()
    {

        $limit = 100;
        $page = $this->request->param('page');
        $offset = ($page - 1) * $limit;
        $userModel = new \app\admin\model\User();

        $list = $userModel
            ->order(['createtime' => 'desc'])->limit($offset, $limit)
            ->select();


        $userRelationModel = new Userrelation();

        foreach ($list as $key => $val) {

            $uid = $val['id'];
            $count = $userModel->where('ref_uid', $uid)->where('group_id', 3)->count('id');
            $userRelationModel->where('uid', $uid)->update([
                'd_two' => $count]);
            $userRelation = $userRelationModel->where('uid', $uid)->find();
            $teams = explode(",", $userRelation['relation']);
            $ids = [];
            foreach ($teams as $v) {
                $ids[] = $v;
            }
            $group_id = $val['group_id'];
            if ($group_id == 3) {
                $where = ['uid' => ['in', $ids]];
                $userRelation->where($where)->setInc('t_two', 1);
            }
        }

        echo "完成";
        echo json_encode($list);

    }

    public function testKey()
    {

        $data = array();
        $obj = array();
        $obj['uid'] = 1;
        $obj['group_id'] = 2;
        $data[] = $obj;

        $obj = array();
        $obj['uid'] = 2;
        $obj['group_id'] = 3;
        $data[] = $obj;

        $obj = array();
        $obj['uid'] = 4;
        $obj['group_id'] = 4;
        $data[] = $obj;


        $obj = array();
        $obj['uid'] = 5;
        $obj['group_id'] = 3;
        $data[] = $obj;

        $obj = array();
        $obj['uid'] = 6;
        $obj['group_id'] = 3;
        $data[] = $obj;


        $rankService = new RankService();
        $list = $rankService->getGroup3List($data);
        echo json_encode($list);

    }


    public function runOrderData()
    {

        $userSalesModel = new Usersales();
        $listoreModel = new Litestoreorder();
        $dayOrderModel = new Orderday();
        $list = $listoreModel->where('order_status', '<>', 'paying')->select();
        foreach ($list as $key=>$val){

           $uid =  $val['seller_id'];
           if($uid != 0){
               $time = $val['createtime'];
               $startDate = date('Y-m-d',$time);
               $startTime = strtotime($startDate." 00:00:00");
               $sale = $dayOrderModel->where('uid',$uid)->where('createtime',$startTime)->find();
               if($sale){
                   $dayOrderModel->where('id',$sale['id'])->setInc('orderNum',1);
                   $dayOrderModel->where('id',$sale['id'])->setInc('orderAmount',$val['pay_price']);
               }else{
                   $dayOrderModel->insert(['uid'=>$uid,'orderNum'=>1,'orderAmount'=>$val['pay_price'],'createtime'=>$startTime]);
               }
           }
        }

        $this->success("完成");
    }

    public function runSelfData(){
        $dayOrderModel = new Orderday();
        $list = $dayOrderModel->select();
        $userSalesModel = new Usersales();
        foreach ($list as $key=>$val){
            $time = $val['createtime'];
            $startDate = date('Y-m',$time);
            $startTime = strtotime($startDate."-01 00:00:00");
            $sale = $userSalesModel->where('uid',$val['uid'])->where('createtime',$startTime)->find();
            if($sale){
                $userSalesModel->where('id',$sale['id'])->setInc('month',$val['orderAmount']);
                $userSalesModel->where('id',$sale['id'])->setInc('all',$val['orderAmount']);
            }else{

                $sale2 = $userSalesModel->where('uid',$val['uid'])->order('createtime','desc')->find();
                if($sale2){
                    $all = $sale2['all']+$val['orderAmount'];
                    $month = $val['orderAmount'];
                }else{
                    $all = $val['orderAmount'];
                    $month = $val['orderAmount'];
                }

                $userSalesModel->insert(['uid'=>$val['uid'],'month'=>$month,'all'=>$all,'createtime'=>$startTime]);
            }
        }

        $this->success('完成');
    }

    public function runTeamData(){


        $dayOrderModel = new Orderday();
        $userSalesModel = new Usersales();
        $list = $userSalesModel->select();
        $userRelationModel = new Userrelation();
        foreach ($list as $key=>$val){

            $startDate = date('Y-m',$val['createtime']);
            $startTime = strtotime($startDate."-01 00:00:00");
            $uid = $val['uid'];
            $relation = $userRelationModel->where('uid',$uid)->find();
            $ids = explode(",",$relation['relation']);
            error_log($uid."==========".$relation['relation']);

            $where = ['uid' => ['in', $ids]];
            $userSalesModel->where($where)->setInc('teamAll', $val['all']);

            if($val['createtime'] >= $startTime){
                $userSalesModel->where($where)->setInc('teamMonth', $val['month']);

            }

        }

        $this->success('完成');


    }

    public function runOrderSta(){

        $userSalesModel = new Usersales();
        $listoreModel = new Litestoreorder();
        $userRelationModel = new Userrelation();
        $list = $listoreModel->where('order_status', '<>', 'paying')->select();
        foreach ($list as $key=>$val) {

            $uid = $val['seller_id'];
            if ($uid != 0) {
                $time = $val['createtime'];
                $startDate = date('Y-m',$time);
                error_log("====runOrderSta=====".$startDate);
                $startTime = strtotime($startDate."-01 00:00:00");
                $sale = $userSalesModel->where('uid',$uid)->where('createtime',$startTime)->find();
                if($sale){
                    $userSalesModel->where('id',$sale['id'])->setInc('month',$val['pay_price']);
                    $userSalesModel->where('id',$sale['id'])->setInc('all',$val['pay_price']);
                }else{
                    $sale2 = $userSalesModel->where('uid',$uid)->order('createtime','desc')->find();
                    if($sale2){
                        $all = $sale2['all']+$val['pay_price'];
                        $teamAll = $sale2['teamAll'];
                        $teamMonth = $sale2['teamMonth'];
                    }else{
                        $all = $val['pay_price'];
                        $teamMonth = 0;
                        $teamAll = 0;
                    }
                    $userSalesModel->insert(['uid'=>$uid,'month'=>$val['pay_price'],'all'=>$all,'teamMonth'=>$teamMonth,'teamAll'=>$teamAll,'createtime'=>$startTime]);
                }
//                $relation = $userRelationModel->where('uid',$uid)->find();
//                $rel = $relation['relation'];
//                $ids = explode(',',$rel);
//                foreach ($ids as $k=>$v){
//                    $s = $userSalesModel->where('uid',$uid)->where('createtime',$startTime)->find();
//                    if($s){
//                        $userSalesModel->where('id',$s['id'])->setInc('teamMonth',$val['pay_price']);
//                        $userSalesModel->where('id',$s['id'])->setInc('teamAll',$val['pay_price']);
//                    }else{
//                        $sale3 = $userSalesModel->where('uid',$uid)->order('createtime','desc')->find();
//                        $teamMonth = $val['pay_price'];
//                        $teamAll = $sale3['teamAll']+$val['pay_price'];
//                        $userSalesModel->insert(['uid'=>$v,'month'=>$sale3['month'],'all'=>$sale3['all'],'teamMonth'=>$teamMonth,'teamAll'=>$teamAll,'createtime'=>$startTime]);
//
//                    }
//
//                }

            }
        }

        $this->success('完成');

    }
}
